package com.bac.rentmap.ad.flat.security;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Generate random security id. Collision is possible but with very low
 * probability.
 * 
 * @author Andrew Osipenko
 */
public class SecureIdGenerator {
	private static final int SECURITY_ID_LENGTH = 32;

	private char[] c;
	private Random random;

	public SecureIdGenerator() {
		List<Character> list = new ArrayList<Character>();
		for (char i = 'a'; i <= 'z'; i++) {
			list.add(i);
		}
		for (char i = 'A'; i <= 'Z'; i++) {
			list.add(i);
		}
		for (char i = '0'; i < '9'; i++) {
			list.add(i);
		}
		c = new char[list.size()];
		for (int i = 0; i < list.size(); i++) {
			c[i] = list.get(i);
		}
		random = new Random();
	}

	public String generate() {
		StringBuilder res = new StringBuilder();
		for (int i = 0; i < SECURITY_ID_LENGTH; i++) {
			res.append(c[random.nextInt(c.length)]);
		}
		return res.toString();
	}
}
